Skip to content

[CoreCLR] link app native runtime/library statically #9938

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 43 commits into
base: main
Choose a base branch
from

Conversation

grendello
Copy link
Contributor

No description provided.

@grendello grendello force-pushed the dev/grendel/clr-host-static-link branch 7 times, most recently from e21e634 to 4e02da2 Compare March 27, 2025 11:52
@grendello grendello force-pushed the dev/grendel/clr-host-static-link branch from e15ccf6 to 9026080 Compare March 28, 2025 17:25
@grendello grendello force-pushed the dev/grendel/clr-host-static-link branch 3 times, most recently from 4aff532 to 0b7bb8c Compare April 2, 2025 20:47
@grendello grendello force-pushed the dev/grendel/clr-host-static-link branch 3 times, most recently from 96d8326 to 39031e6 Compare April 14, 2025 07:07
@grendello grendello force-pushed the dev/grendel/clr-host-static-link branch 5 times, most recently from 7cdfb21 to 86691d0 Compare April 28, 2025 07:53
jonpryor pushed a commit that referenced this pull request Apr 29, 2025
Context: #9938

PR #9938 is an attempt to build `libnet-android.Debug.so` and
`libnet-android.Release.so` so that CoreCLR is *statically linked* in
to `libnet-android*.so`.  This reduces the number of native libraries
that must be placed into the `.apk`, and allows the `.apk` to be
smaller, as the native linker can remove unused code.

*Note*: CoreCLR is ***not*** yet statically linked into
`libnet-android*.so`.  This is *preliminary* work to support that.

This iteration of static linking also requires that *all* symbols be
resolvable, by requiring `lld --no-undefined` by default.  This in
turn requires that we ship *copies* of `libc.so` et al, so that *all*
symbols needed by `libnet-android*.so` can be resolved, including
those from the Android NDK.

Update `build-tools/xaprepare` to extract required native libraries
from the NDK, and add those to the .NET for Android Workload packs.

Update `Xamarin.Android.Build.Tasks.dll` to cleanup and refactor
`ld` invocation, so that `libnet-android*.so` is built with
`ld --no-undefined`.  This makes it easier to maintain and update the
linker code in the future, as well as ensures that we use consistent
linking flags across the board.

Updates the LLVM IR code generator bits, also extracted from #9938,
which aren't strictly related to native linking, but they are placed
here in order to make #9938 smaller.
@grendello grendello force-pushed the dev/grendel/clr-host-static-link branch 2 times, most recently from 96649e1 to 75e6902 Compare May 6, 2025 08:49
@grendello grendello force-pushed the dev/grendel/clr-host-static-link branch 3 times, most recently from 29ac51a to bfc4af0 Compare May 16, 2025 10:54
@grendello grendello force-pushed the dev/grendel/clr-host-static-link branch 2 times, most recently from 9327572 to 3ba61c3 Compare May 26, 2025 09:12
@grendello grendello requested a review from Copilot May 26, 2025 12:31
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces support for statically linking the .NET native runtime (CoreCLR/Mono) into Android applications by adding new MSBuild tasks, target definitions, and code paths to generate and preserve P/Invoke tables.

  • Adds a new LinkNativeRuntime task to perform native runtime linking per ABI.
  • Extends existing code generators (GenerateNativeMarshalMethodSources, GenerateJavaStubs, etc.) with an EnableNativeRuntimeLinking flag and P/Invoke preservation logic.
  • Updates MSBuild targets and xaprepare scripts to include native runtime assets and configuration properties.

Reviewed Changes

Copilot reviewed 42 out of 42 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/Xamarin.Android.Build.Tasks/Tasks/PrepareAbiItems.cs Added a new mode branch for runtime linking.
src/Xamarin.Android.Build.Tasks/Tasks/LinkNativeRuntime.cs Introduced the LinkNativeRuntime AsyncTask.
src/Xamarin.Android.Build.Tasks/Tasks/GenerateNativeMarshalMethodSources.cs Added EnableNativeRuntimeLinking path.
src/Xamarin.Android.Build.Tasks/Tasks/GenerateMainAndroidManifest.cs Exposed EnableNativeRuntimeLinking property.
Configuration.props Defined shared _MonoRuntimeFlavorDirName and _CLRRuntimeFlavorDirName and duplicated _RuntimeRedistDirName.
Comments suppressed due to low confidence (2)

src/Xamarin.Android.Build.Tasks/Tasks/PrepareAbiItems.cs:57

  • The mode string "runtime_linking" does not match the constant name "pinvoke_preserve". Consider renaming one of them for consistency (e.g., use "pinvoke_preserve" as the mode or rename the constant to "runtime_linking").
} else if (String.Compare ("runtime_linking", Mode, StringComparison.OrdinalIgnoreCase) == 0) {

src/Xamarin.Android.Build.Tasks/Tasks/GenerateMainAndroidManifest.cs:28

  • This property is declared but not referenced within this task. Remove it or implement its intended behavior to avoid dead code.
public bool EnableNativeRuntimeLinking { get; set; }

@grendello grendello force-pushed the dev/grendel/clr-host-static-link branch from ee55bab to e0093df Compare May 27, 2025 12:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant